*********************************************************************************
*							Estimate Bunching Elasticity - Financial score	*
*							Jennifer Mayo									*
*							Date: 21/02/2020									*
*********************************************************************************


clear all
set more off

global name "jmp4k"
global home "/Users/$name/Library/CloudStorage/OneDrive-UniversityofMissouri/Projects/Navigating the notches/"
global data "$home/Replication"
global output "$home/CN Output"


use "$data/Fin score.dta", clear


*********************************************************************************
*	Collapse Data into counts of firms by ratio bins							*
*********************************************************************************


	drop if PublicationYear==2011  
	drop if PublicationYear==2016  // drop years method changed
	duplicates drop

	egen double scoregroups = cut(finscore), at(0 (0.2) 100)
	
//	gen wt = round(finscore,1) // create an integer weight to use as frequency weights
	
	
		
*********************************************************************************
*	Define Programs to Estimate Bunching and Bootstrap SE				*
*********************************************************************************
	
	* Define Bunching Estimate Program: 

	do "$data/finbunchest.do" // Create bunching estimator program

	* Define Bootstrap Estimate Program: 
	
	cap program drop bunchestse
	program define bunchestse, rclass
		args ub threshold
		
		preserve
		
		bsample, cluster(ein)
		finbunchest `ub' `threshold'
		

		return scalar lb_est = r(lb_est)
			return scalar excess_est = r(excess_est)
		return scalar b_av_est = r(b_av_est)				
		
	restore
	end	
	

*********************************************************************************
*	Estimate Bunching on all charities, 4* threshold, all years/annual 		*
********************************************************************************* 

	* Restrict sample: 

	preserve
	drop if finscore<83
	
	count
	local n_1 = r(N)
	file open myfile using "$output/fin_bunching_estimates_92obstotal.tex", write text replace 
	file write myfile "`n_1'\unskip" 
	file close myfile
	
	finbunchest 92 90  // args: ub and threshold
	mat res1 = (r(lb_est),r(excess_est),r(b_av_est))
	
	matlist res1
	
	restore 

	*Bootstrap SE: 
	
	preserve
	drop if finscore<83
//	keep if PublicationYear == 2015	
	
	simulate lb=r(lb_est) excess = r(excess_est) b_av = r(b_av_est), reps(100) seed(1234): bunchestse 92 90
	
	bstat, stat(res1) n(`n_1')
	
	mat A_1 = r(table)
	
	restore
	
* 2019	
	
	preserve
	drop if finscore<83
	keep if PublicationYear == 2019
	
	count
	local n_2 = r(N)
	file open myfile using "$output/fin_bunching_estimates_92obs2019.tex", write text replace 
	file write myfile "`n_2'\unskip" 
	file close myfile
	
	finbunchest 92 90  // args: ub and threshold
	mat res2 = (r(lb_est),r(excess_est),r(b_av_est))
	
	matlist res2
	
	restore 

	*Bootstrap SE: 
	
	preserve
	drop if finscore<83
	keep if PublicationYear == 2019
	
	simulate lb=r(lb_est) excess = r(excess_est) b_av = r(b_av_est), reps(100) seed(1234): bunchestse 92 90
	
	bstat, stat(res2) n(`n_1')
	
	mat A_2 = r(table)
	
	restore
	
* 2018	
	
	preserve
	drop if finscore<83
	keep if PublicationYear == 2018
	
	count
	local n_3 = r(N)
	file open myfile using "$output/fin_bunching_estimates_92obs2018.tex", write text replace 
	file write myfile "`n_3'\unskip" 
	file close myfile
	
	finbunchest 92 90  // args: ub and threshold
	mat res3 = (r(lb_est),r(excess_est),r(b_av_est))
	
	matlist res3
	
	restore 

	*Bootstrap SE: 
	
	preserve
	drop if finscore<83
	keep if PublicationYear == 2018
	
	simulate lb=r(lb_est) excess = r(excess_est) b_av = r(b_av_est), reps(100) seed(1234): bunchestse 92 90
	
	bstat, stat(res3) n(`n_1')
	
	mat A_3 = r(table)
	
	restore
	
* 2017	
	
	preserve
	drop if finscore<83
	keep if PublicationYear == 2017
	
	count
	local n_4 = r(N)
	file open myfile using "$output/fin_bunching_estimates_92obs2017.tex", write text replace 
	file write myfile "`n_4'\unskip" 
	file close myfile
	
	finbunchest 92 90  // args: ub and threshold
	mat res4 = (r(lb_est),r(excess_est),r(b_av_est))
	
	matlist res4
	
	restore 

	*Bootstrap SE: 
	
	preserve
	drop if finscore<83
	keep if PublicationYear == 2017
	
	simulate lb=r(lb_est) excess = r(excess_est) b_av = r(b_av_est), reps(100) seed(1234): bunchestse 92 90
	
	bstat, stat(res4) n(`n_1')
	
	mat A_4 = r(table)
	
	restore	
	
	
* 2015	
	
	preserve
	drop if finscore<83
	keep if PublicationYear == 2015
	
	count
	local n_5 = r(N)
	file open myfile using "$output/fin_bunching_estimates_92obs2015.tex", write text replace 
	file write myfile "`n_5'\unskip" 
	file close myfile
	
	finbunchest 92 90  // args: ub and threshold
	mat res5 = (r(lb_est),r(excess_est),r(b_av_est))
	
	matlist res5
	
	restore 

	*Bootstrap SE: 
	
	preserve
	drop if finscore<83
	keep if PublicationYear == 2015
	
	simulate lb=r(lb_est) excess = r(excess_est) b_av = r(b_av_est), reps(100) seed(1234): bunchestse 92 90
	
	bstat, stat(res5) n(`n_1')
	
	mat A_5 = r(table)
	
	restore
	
*********************************************************************************
* Create LateX Table for Paper												*
*********************************************************************************

	*All years pt. estimates: 
	
	mat r1 =  [round(A_1[1,3],.001)]
	
	* 2019 pt. estimates: 
	
	mat r2 =  [round(A_2[1,3],.001)]
	
	* 2018 pt. estimates: 
	
	mat r3 =  [round(A_3[1,3],.001)]
	
	* 2017 pt. estimates: 
	
	mat r4 =  [round(A_4[1,3],.001)]
	
	* 2015 pt. estimates: 
	
	mat r5 =  [round(A_5[1,3],.001)]
	
	* All years standard errors: 
	
	mat r11 =  [round(A_1[2,3],.001)]

	* 2019 standard errors: 
	
	mat r12 =  [round(A_2[2,3],.001)]
	
	* 2018 standard errors: 
	
	mat r13 =  [round(A_3[2,3],.001)]
	
	* 2017 standard errors: 
	
	mat r14 =  [round(A_4[2,3],.001)]
	
	* 2015 standard errors: 
	
	mat r15 =  [round(A_5[2,3],.001)]


	estadd matrix r1, replace
	estadd matrix r2, replace
	estadd matrix r3, replace
	estadd matrix r4, replace
	estadd matrix r5, replace
	estadd matrix r11, replace
	estadd matrix r12, replace
	estadd matrix r13, replace
	estadd matrix r14, replace
	estadd matrix r15, replace
	
	esttab using "$output/fin_bunching_estimates_92.tex", ///
		cells("r1 r2 r3 r4 r5" "r11(par) r12(par) r13(par) r14(par) r15(par)") collabels("All Years" "2019" "2018" "2017" "2015") ///
 		coeflabel(c1 "Average bunching response" )   nomtitles ///
		nonumbers gaps booktab frag plain replace
	
	
	
	
	
	
	
	


